const express = require('express');
const homeRouter = express.Router();

// const MongoClient = require('mongodb').MongoClient;
// const URL = 'mongodb://localhost:27017';
// const dbName = 'SuperMall';
const DB = require('../DataConn/DB')


homeRouter.use((req, res, next) => {
  //console.log(new Date(Date.now()), `请求路由/home及其子路由`);
  next();
})


homeRouter.get('/', (req, res, next) => {
  res.json({
    status: 200,
    data: "请求成功"
  })
});

//返回首页的swiper图片的地址
homeRouter.get('/swipers', (req, res, next) => {
  let swipers = [{
      name: 'swiper',
      data: [{
          src: 'http://localhost:3000/static/images/swiper/2.jpg',
          link: 'http://localhost:3000/'
        },
        {
          src: 'http://localhost:3000/static/images/swiper/3.jpg',
          link: 'http://localhost:3000/'
        },
        {
          src: 'http://localhost:3000/static/images/swiper/4.jpg',
          link: 'http://localhost:3000/'
        },
        {
          src: 'http://localhost:3000/static/images/swiper/5.jpg',
          link: 'http://localhost:3000/'
        }
      ]
    },
    {
      name: 'list',
      data: [{
          src: 'http://localhost:3000/static/images/swiper/list/skirt.png',
          link: 'http://localhost:8080/cart',
          title: '短裙'
        },
        {
          src: 'http://localhost:3000/static/images/swiper/list/clild.png',
          link: 'http://localhost:8080/cart',
          title: '童装'
        },
        {
          src: 'http://localhost:3000/static/images/swiper/list/shoe.png',
          link: 'http://localhost:8080/cart',
          title: '鞋子'
        },
        {
          src: 'http://localhost:3000/static/images/swiper/list/T-shirt.png',
          link: 'http://localhost:8080/cart',
          title: '上衣'
        }
      ]
    }
  ]
  res.send(swipers);
});

//查询首页 Products表中的所有商品数据
homeRouter.get('/goods', (req, res, next) => {
  //创建MongoClient客户端连接
  // const client = new MongoClient(URL, {
  //   useNewUrlParser: true,
  //   useUnifiedTopology: true
  // });
  // client.connect(err => {
  //   if (err) {
  //     console.log("error Messages is" + err.stack);
  //     return;
  //   }
  //   //console.log("连接服务器成功");
  //   const db = client.db(dbName);
  //   mydb.findDocumentsLimit(db,String(req.query.type),Number(req.query.page*40), (data) => {
  //     console.log("本次请求/home/goods type为:",req.query.type);
  //     console.log("请求/home/goods page为:",req.query.page);
  //     console.log('————————————————————————————————————')
  //     res.json({
  //       status: 200,
  //       type:req.query.type,
  //       list: data ? data : "没有数据"
  //     });
  //     client.close()
  //   })
  // });

  DB.findDocumentsLimit(String(req.query.type),Number(req.query.page*40),data=>{
    console.log("本次请求/home/goods type为:",req.query.type);
        console.log("请求/home/goods page为:",req.query.page);
        console.log('————————————————————————————————————')
        res.json({
          status: 200,
          type:req.query.type,
          list: data ? data : "没有数据"
        });
  })
})

module.exports = homeRouter;